//
//  CJInterstitialAdapter.m
//  Pods
//
//  Created by ly on 2024/3/4.
//

#import "CJInterstitialAdapter.h"
#import "CJInterstitialCustomEvent.h"
#import "CJAdapterHeader.h"
#import <AnyThinkSDK/AnyThinkSDK.h>
#import "CJATTMBiddingManager.h"
#import "CJCommonTool.h"
#import "CJAdapterSetup.h"

@interface CJInterstitialAdapter()

@property (nonatomic, strong) CJInterstitialCustomEvent *customEvent;
@property (nonatomic, strong) CJInterstitialAd *interstitialAd;

@end

@implementation CJInterstitialAdapter

-(instancetype) initWithNetworkCustomInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo {
    if (self = [super init]) {
//        [CJAdapterSetup setupCJSDKWithAppId:@"appid"];
    }
    return self;
}

-(void)loadADWithInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo completion:(void (^)(NSArray<NSDictionary *> *, NSError *))completion {
    
    _customEvent = [[CJInterstitialCustomEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
    _customEvent.requestCompletionBlock = completion;
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:serverInfo[@"unitid"]];
    //    _customEvent.customEventMetaDataDidLoadedBlock = self.metaDataDidLoadedBlock;
    if (request) {
        if (request.customObject != nil) { // load secced
            self.interstitialAd = request.customObject;
            self.interstitialAd.delegate = _customEvent;
            [_customEvent trackInterstitialAdLoaded:self.interstitialAd adExtra:@{}];
        } else { // fail
            NSError *error = [NSError errorWithDomain:ATADLoadingErrorDomain code:ATAdErrorCodeThirdPartySDKNotImportedProperly userInfo:@{NSLocalizedDescriptionKey:@"AT has failed to load native.", NSLocalizedFailureReasonErrorKey:@"It took too long to load placement stragety."}];
            [_customEvent trackInterstitialAdLoadFailed:error];
        }
        [[CJATTMBiddingManager sharedInstance] removeRequestItmeWithUnitID:serverInfo[@"unitid"]];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.interstitialAd = [[CJInterstitialAd alloc] initWithResourceId:serverInfo[@"unitid"]];
            self.interstitialAd.delegate = self.customEvent;
            [self.interstitialAd loadAdData];
        });
    }
}

+ (BOOL)adReadyWithCustomObject:(id)customObject info:(NSDictionary*)info {
    return (CJInterstitialAd *)customObject ? true : false;
}

+ (void)showInterstitial:(ATInterstitial*)interstitial inViewController:(UIViewController*)viewController delegate:(id<ATInterstitialDelegate>)delegate {
    CJInterstitialAd *interstitialAd = interstitial.customObject;
    interstitial.customEvent.delegate = delegate;
    [interstitialAd showFromRootViewController:viewController];
}

+ (void)bidRequestWithPlacementModel:(ATPlacementModel *)placementModel unitGroupModel:(ATUnitGroupModel *)unitGroupModel info:(NSDictionary *)info completion:(void(^)(ATBidInfo *bidInfo, NSError *error))completion {
   if (NSClassFromString(@"CJADManager") == nil) {
       if (completion != nil) { completion( nil, [NSError errorWithDomain:@"com.ubix.mediation.ios" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Bid request has failed", NSLocalizedFailureReasonErrorKey:@"CJAD is not imported"}]); }
       return;
   }
    [CJADManager configure:info[@"appid"]];
    CJATTMBiddingManager *biddingManage = [CJATTMBiddingManager sharedInstance];
    CJATTMBiddingRequest *request = [CJATTMBiddingRequest new];
    request.unitGroup = unitGroupModel;
    request.placementID = placementModel.placementID;
    request.bidCompletion = completion;
    request.unitID = info[@"unitid"];
    request.extraInfo = info;
    request.adType = ESCAdFormatInterstitial;
    dispatch_async(dispatch_get_main_queue(), ^{
        CJInterstitialAd *interstitialAd = [[CJInterstitialAd alloc] initWithResourceId: info[@"unitid"]];
        request.customObject = interstitialAd;
        [biddingManage startWithRequestItem:request];
        [interstitialAd loadAdData];
    });
}

+ (void)sendWinnerNotifyWithCustomObject:(id)customObject secondPrice:(NSString*)price userInfo:(NSDictionary<NSString *, NSString *> *)userInfo {
    CJSplashAd *splashAd = (CJSplashAd *)customObject;
    NSLog(@"ly---ad----win");
}

+ (void)sendLossNotifyWithCustomObject:(nonnull id)customObject lossType:(ATBiddingLossType)lossType winPrice:(nonnull NSString *)price userInfo:(NSDictionary *)userInfo {
    NSLog(@"ly---ad----failed");
}

@end
